﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fortuitus.Core;

namespace ConanLib
{
    /// <summary>
    /// An item that defines a relationship between two features
    /// </summary>
    public class ClusterItem
    {
        /// <summary>
        /// The first feature.
        /// </summary>
        public IVector Feature1 { get; set; }

        /// <summary>
        /// The second feature.
        /// </summary>
        public IVector Feature2 { get; set; }

        /// <summary>
        /// An value that weights the relationship between Feature1 and Feature2
        /// E.g. Pearson Correlation Coefficient
        /// </summary>
        public double RelationshipValue { get; set; }

        /// <summary>
        /// The fully parameterized ctor.
        /// </summary>
        /// <param name="feature1">The first feature of the relationship.</param>
        /// <param name="feature2">The second feature of the relationship.</param>
        /// <param name="rel">The weight for the relationship.</param>
        public ClusterItem(IVector feature1, IVector feature2, double rel)
        {
            Feature1 = feature1;
            Feature2 = feature2;
            RelationshipValue = rel;
        }


        public override bool Equals(object obj)
        {

            ClusterItem c = (ClusterItem)obj;
            return
                (this.Feature1.GetValue<int>("Id") == c.Feature1.GetValue<int>("Id")
                && this.Feature2.GetValue<int>("Id") == c.Feature2.GetValue<int>("Id"))
                ||
                (this.Feature1.GetValue<int>("Id") == c.Feature2.GetValue<int>("Id")
                && this.Feature2.GetValue<int>("Id") == c.Feature1.GetValue<int>("Id"));
        }


        /// <summary>
        /// Determines whether this cluster item has an overlap with another cluster item.
        /// </summary>
        /// <param name="cluster">The cluster item to check for an overlap.</param>
        /// <returns>An values that indicates wheter there is an overlap or not.</returns>
        public bool HasOverlap(ClusterItem cluster)
        {
            return this.Feature1.GetValue<int>("Id") == cluster.Feature1.GetValue<int>("Id")
                            || this.Feature1.GetValue<int>("Id") == cluster.Feature2.GetValue<int>("Id")
                            || this.Feature2.GetValue<int>("Id") == cluster.Feature1.GetValue<int>("Id")
                            || this.Feature2.GetValue<int>("Id") == cluster.Feature2.GetValue<int>("Id");
        }

        public override int GetHashCode()
        {
            return Feature1.GetValue<int>("Id") ^ Feature2.GetValue<int>("Id");
        }

        public override string ToString()
        {
            if (Feature1.HasValue("Name") && Feature2.HasValue("Name"))
                return Feature1.GetValue<string>("Name") + Feature1.GetValue<int>("Id") + "<->" + Feature2.GetValue<int>("Id") + Feature2.GetValue<string>("Name");
            else
                return base.ToString();
        }
    }
}
